﻿using GalaSoft.MvvmLight;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using GalaSoft.MvvmLight.Command;
using CommonServiceLocator;
using System.Windows.Forms;
using System.Windows;
using System.Collections.ObjectModel;
using TimerJobServer.Common;
using TimerJobServer.Common.Mina;

namespace TimerJobServerManager.ViewModel
{
    public class AddJobViewModel : ViewModelBase
    {
        private string _JobName = Guid.NewGuid().ToString("N");

        public string JobName
        {
            get { return _JobName; }
            set
            {
                Set("JobName", ref _JobName, value);
            }
        }


        private string _JobCron = "15 0/1 * * * ?";

        public string JobCron
        {
            get { return _JobCron; }
            set
            {
                Set("JobCron", ref _JobCron, value);
            }
        }

        private string _JobPath;

        public string JobPath
        {
            get { return _JobPath; }
            set
            {
                Set("JobPath", ref _JobPath, value);
            }
        }

        private string _JobClassName;

        public string JobClassName
        {
            get { return _JobClassName; }
            set
            {
                Set("JobClassName", ref _JobClassName, value);
            }
        }

        private string _JobMethodName;

        public string JobMethodName
        {
            get { return _JobMethodName; }
            set
            {
                Set("JobMethodName", ref _JobMethodName, value);
            }
        }

        private QuarzJobType _JobType;

        public QuarzJobType JobType
        {
            get { return _JobType; }
            set
            {
                Set("JobType", ref _JobType, value);
                switch (_JobType)
                {
                    case QuarzJobType.Dll:
                        TabSelectIndex = 0;
                        break;
                    //case QuarzJobType.CS:
                    //    DLLVisable = Visibility.Visible;
                    //    ExeVisable = Visibility.Visible;
                    //    break;
                    case QuarzJobType.Exe:
                        TabSelectIndex = 1;
                        break;
                    case QuarzJobType.HttpGet:
                        TabSelectIndex = 2;
                        break;
                    case QuarzJobType.HttpPost:
                        TabSelectIndex = 3;
                        break;
                    default:
                        break;
                }
            }
        }

        private int _TabSelectIndex=0;

        public int TabSelectIndex
        {
            get { return _TabSelectIndex; }
            set
            {
                Set("TabSelectIndex", ref _TabSelectIndex, value);
            }
        }

        private Array _JobTypeSource = Enum.GetValues(typeof(QuarzJobType));

        public Array JobTypeSource
        {
            get { return _JobTypeSource; }
        }

        private bool _JobEnable = true;

        public bool JobEnable
        {
            get { return _JobEnable; }
            set
            {
                Set("JobEnable", ref _JobEnable, value);
            }
        }
        
        private string _ExeArgs;

        public string ExeArgs
        {
            get { return _ExeArgs; }
            set
            {
                Set("ExeArgs", ref _ExeArgs, value);
            }
        }

        private string _HttpUrl;

        public string HttpUrl
        {
            get { return _HttpUrl; }
            set
            {
                Set("HttpUrl", ref _HttpUrl, value);
            }
        }

        private string _HttpPostData;

        public string HttpPostData
        {
            get { return _HttpPostData; }
            set
            {
                Set("HttpPostData", ref _HttpPostData, value);
            }
        }

        private ObservableCollection<HttpHeader> _HttpHeaderDic = new ObservableCollection<HttpHeader>()
        {
            new HttpHeader(){Key="Content-Type",Value="application/json" }
        };

        public ObservableCollection<HttpHeader> HttpHeaderDic
        {
            get { return _HttpHeaderDic; }
            set
            {
                Set("HttpHeaderDic", ref _HttpHeaderDic, value);
            }
        }


        public ICommand JobAddCommand { get; set; }
        public ICommand JobUpdateCommand { get; set; }
        public ICommand ChosePathCommand { get; set; }
        public ICommand CronChangeCommand { get; set; }



        /// <summary>
        /// 或者主界面功能
        /// </summary>
        private MainViewModel main = ServiceLocator.Current.GetInstance<MainViewModel>();
        public AddJobViewModel()
        {
            JobAddCommand = new RelayCommand(AddJob);
            JobUpdateCommand = new RelayCommand(UpdateJob, new Func<bool>(CanUpdateJob));
            ChosePathCommand = new RelayCommand(ChosePath);
            CronChangeCommand = new RelayCommand<string>(CronChange);
            JobType = QuarzJobType.Dll;//默认为dll类型
        }

        /// <summary>
        /// 更改Cron表达式
        /// </summary>
        /// <param name="obj"></param>
        private void CronChange(string obj)
        {
            if (obj!=null)
            {
                JobCron = obj;
            }
        }

        private void ChosePath()
        {
            OpenFileDialog dlg = new OpenFileDialog()
            {
                Filter= "可执行程序|*.cs;*.exe;*.dll"
            };

            if (dlg.ShowDialog()==DialogResult.OK)
            {
                JobPath=dlg.FileName;;
            }
        }

        private bool CanUpdateJob()
        {
            if (main.SelectJob != null && main.SelectJob.ID != null)
            {
                return true;
            }
            return false;
        }


        /// <summary>
        /// 更新
        /// </summary>
        private void UpdateJob()
        {
            if (main.SelectJob != null)
            {
                var job = main.SelectJob;
                QuarzJobInfo quarzJobInfo = new QuarzJobInfo()
                {
                    ID = main.SelectJob.ID,
                    Name = JobName,
                    ClassFullName = JobClassName,
                    Cron = JobCron,
                    Enable = JobEnable,
                    MethodName = JobMethodName,
                    FullPath = JobPath,
                    JobType = JobType,
                    Args = ExeArgs,
                    HttpHeaderDic = new List<HttpHeader>(),
                    HttpURL = HttpUrl,
                    HttpPostDate = HttpPostData
                };

                foreach (var item in HttpHeaderDic)
                {
                    quarzJobInfo.HttpHeaderDic.Add(item);
                }

                TransferObj transfer = new TransferObj()
                {
                    Type = TranType.UpdateJob,
                    Job = quarzJobInfo
                };
                main.SockSend(transfer);
            }
        }

        /// <summary>
        /// 添加任务
        /// </summary>
        private void AddJob()
        {
            QuarzJobInfo quarzJobInfo = new QuarzJobInfo
            {
                ID = Guid.NewGuid().ToString("N"),
                Name = JobName,
                ClassFullName = JobClassName,
                Cron = JobCron,
                Enable = JobEnable,
                MethodName = JobMethodName,
                FullPath = JobPath,
                JobType = JobType,
                Args = ExeArgs,
                HttpHeaderDic = new List<HttpHeader>(),
                HttpURL = HttpUrl,
                HttpPostDate = HttpPostData
            };

            foreach (var item in HttpHeaderDic)
            {
                quarzJobInfo.HttpHeaderDic.Add(item);
            }

            TransferObj transfer = new TransferObj()
            {
                Type = TranType.AddJob,
                Job = quarzJobInfo
            };
            main.SockSend(transfer);
        }
    }
}
